package com.jingan.nursinghome.regulatory.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.dialog.micrometertable.CreateMicrometerTableDailog;
import com.jingan.nursinghome.regulatory.dialog.micrometertable.MicrometerTableOSDialog;
import com.jingan.nursinghome.regulatory.dialog.micrometertable.XuantiDialog;
import com.jingan.nursinghome.regulatory.view.PageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 彭治铭 on 2017/10/17.
 */

public class PageAdapter extends RecyclerView.Adapter<PageAdapter.MyViewHolder> {

    private int max, count, maxPage;

    public PageAdapter(int max, int count, int maxPage) {
        this.max = max;
        this.count = count;
        this.maxPage = maxPage;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(View itemView) {
            super(itemView);
        }
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_micrometertablleos_item, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。

        return new MyViewHolder(new PageView(parent.getContext()));
        //return new MyViewHolder(view);
    }

    List<Integer> index = new ArrayList<>();

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final PageView pageView = (PageView) holder.itemView;
        int start = 1;
        if (position == 0) {
            start = 1;
        } else {
            start = index.get(position - 1);
        }
        int end = start + count - 1;
        if (end > max) {
            end = max;
        }
        if (index.size() <= position) {//防止重复错乱加载。
            index.add(position, end);
        }
        final int start2 = start;

        boolean isscore = true;
//        Log.e("test","start:\t"+start+"\tend:\t"+end);
        for (int i = start; i <= end; i++) {
            String c =MicrometerTable.getInstance().getTopicinfoList().get(i - 1).getPscore();
            if (c == null || c.equals("")) {
                isscore = false;
            }
        }
        pageView.setPage(start + "", end + "", (MicrometerTableOSDialog.pageIndex >= start && MicrometerTableOSDialog.pageIndex < start + 2), isscore);
        pageView.setEnabled(true);
        pageView.setClickable(true);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (XuantiDialog.xuantiDialog != null && XuantiDialog.xuantiDialog.mBaseListenner != null) {
                    XuantiDialog.xuantiDialog.mBaseListenner.onResult(start2);
                    XuantiDialog.xuantiDialog.dismiss();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return maxPage;
    }
}
